﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using cecbData;
using cecbClasses.Data_Adapters.ImpactAndActivity;
using cecbClasses.Data_Adapters;

namespace CECB.Activity
{
    /// <summary>
    /// Interaction logic for Project_Activity_Update_Window.xaml
    /// </summary>
    public partial class Project_Activity_Update_Window : Window
    {
        public Project_Activity_Update_Window()
        {
            InitializeComponent();
            init();
        }

        private void init()
        {



            listBoxActivities.ItemsSource = new cecbClasses.Data_Adapters.ImpactAndActivity.Func_Activities().getAllActivities();

            cecbClasses.Data_Adapters.Slider slid = new cecbClasses.Data_Adapters.Slider();

            ProjectAutocompleteTextBox.ItemsSource = slid.getProjectDetails();
            if (listBoxActivities.Items.Count > 0)
            {
                groupBox.Visibility = System.Windows.Visibility.Visible;
            }
            else groupBox.Visibility = System.Windows.Visibility.Hidden;

            textBoxActivityUpdate.IsEnabled = false;
        }


        private void textBoxActivitySearch_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (labelProjectReference.Content.ToString().Length > 0)
                {

                    if (textBoxActivityUpdate.Text.ToString().Trim().Length > 0)
                    {

                        int xx = comboBoxComponents.SelectedIndex;
                        String t1 = "1. Preconstriction & Mobilization";
                        String t2 = "2. Constriction Phase";
                        String t3 = "3. Operation & Maintenance Phase";
                        String pass = "";
                        if (xx == 0)
                            pass = t1;
                        else if (xx == 1)
                            pass = t2;
                        else if (xx == 2)
                            pass = t3;

                        if (new cecbClasses.Data_Adapters.ImpactAndActivity.Func_Activities().deleteProjectActivity(labelProjectReference.Content.ToString(), _actId))
                        {
                            MessageBox.Show("Activity Deleted Successfully", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
                            init();

                            listBoxActivities.ItemsSource = new cecbClasses.Data_Adapters.ImpactAndActivity.Func_Activities().getAllActivities();
                            int x = comboBoxComponents.SelectedIndex;
                            comboBoxComponents.SelectedIndex = -1;//
                            comboBoxComponents.SelectedIndex = x; // -1;


                        }
                        else
                        {
                            MessageBox.Show("Activity Delete Failed", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        }

                    }
                    else
                        MessageBox.Show("Select An Activity ", "Message", MessageBoxButton.OK, MessageBoxImage.Error);

                }
                else
                    MessageBox.Show("Select A Project", "Message", MessageBoxButton.OK, MessageBoxImage.Error);


            }
            catch (Exception)
            {
                MessageBox.Show("Select A Project", "Message", MessageBoxButton.OK, MessageBoxImage.Error);

            }


        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            //    try
            //    {
            //        if (labelProjectReference.Content.ToString().Length > 0)
            //        {

            //            if (textBoxActivityUpdate.Text.ToString().Trim().Length > 0)
            //            {
            //      int xx = comboBoxComponents.SelectedIndex;
            //                String t1 = "Preconstriction & Mobilization";
            //                String t2 = "Constriction Phase";
            //                String t3 = "Operation & Maintenance Phase";
            //                String pass = "";
            //                if (xx == 0)
            //                    pass = t1;
            //                else if (xx == 1)
            //                    pass = t2;
            //                else if (xx == 2)
            //                    pass = t3;
            ////  

            //                if (new Func_Activities().updateProjectActivity(labelProjectReference.Content.ToString(), _actId, pass, textBoxActivityUpdate.Text.ToString()))
            //                {
            //                    MessageBox.Show("Activity Updated Successfully", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
            //                    textBoxActivityUpdate.Clear();
            //                }
            //                else
            //                {

            //                    MessageBox.Show("Activity Update Failed", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            //                }
            //                textBoxActivityUpdate.Clear();
            //                textBoxActivityUpdate.IsEnabled = false;



            //            }
            //            else
            //                MessageBox.Show("Select An Activity ", "Message", MessageBoxButton.OK, MessageBoxImage.Error);

            //        }
            //        else
            //            MessageBox.Show("Select A Project", "Message", MessageBoxButton.OK, MessageBoxImage.Error);


            //    }
            //    catch (Exception)
            //    {

            //        MessageBox.Show("Select A Project", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
            //    }




            cecbClasses.Data_Adapters.ImpactAndActivity.Func_Activities activity = new cecbClasses.Data_Adapters.ImpactAndActivity.Func_Activities();

            if (textBoxActivityUpdate.Text == "")
            {
                MessageBox.Show("Nothing has been modified", "Error", MessageBoxButton.OK, MessageBoxImage.Error);

            }
            else
            {
                if (activity.updateActivity(_actId, textBoxActivityUpdate.Text.ToString(), comboBoxComponents.SelectedValue.ToString()))
                {
                    MessageBox.Show("Project Impact Updated Successfully", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
                    textBoxActivityUpdate.Clear();


                    listBoxActivities.ItemsSource = new cecbClasses.Data_Adapters.ImpactAndActivity.Func_Activities().getAllActivities();
                    int x = comboBoxComponents.SelectedIndex;
                    comboBoxComponents.SelectedIndex = -1;//
                    comboBoxComponents.SelectedIndex = x; // -1;
                    //listBoxActivities.Visibility = System.Windows.Visibility.Hidden;
                }
                else
                {
                    MessageBox.Show("Project Impact Updation failed", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }

            }

        }

        private void NewButton_Click(object sender, RoutedEventArgs e)
        {

        }

        string _actId = "";
        private void listBoxActivities_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // ProjectActivities pac = (ProjectActivities)listBoxActivities.SelectedItem;
            try
            {
                if (listBoxActivities.SelectedIndex > -1)
                {
                    try
                    {
                        textBoxActivityUpdate.IsEnabled = true;
                        cecbData.ProjectActivities x = (cecbData.ProjectActivities)listBoxActivities.SelectedValue;
                        _actId = x.actv_reference.ToString();

                        textBoxActivityUpdate.Text = new cecbClasses.Data_Adapters.ImpactAndActivity.Func_Activities().getActivityNameById(x.actv_reference.ToString());
                    }
                    catch (Exception)
                    {

                        MessageBox.Show("Error", "Select A Project", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
            }
            catch (Exception)
            {

            }
            //  textBoxActivityUpdate.Text = new  cecbData.Activities().actv_name.First(i => i.)
            //ProjectActivities y = (ProjectActivities) listBoxActivities.SelectedItems;
            //ProjectActivities z = (ProjectActivities)listBoxActivities.ItemsSource;

            // textBoxActivityUpdate.Text = pac.actv_name.ToString();
        }

        private void ProjectAutocomplete_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            cecbClasses.Data_Adapters.Slider slid = new cecbClasses.Data_Adapters.Slider();

            ProjectDetailsWrapPanel.DataContext = slid.getProjectDetailsByRef(ProjectAutocompleteTextBox.Text);

            listBoxActivities.ItemsSource = new cecbClasses.Data_Adapters.ImpactAndActivity.Func_Activities().getAllActivities();
           // int x = comboBoxComponents.SelectedIndex;
            comboBoxComponents.SelectedIndex = -1;//
           // comboBoxComponents.SelectedIndex = x; // -1;

        }
        String pass = "";
        private void comboBoxComponents_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

         //   listBoxActivities.Visibility = System.Windows.Visibility.Hidden;
            int indx = comboBoxComponents.SelectedIndex;
            String t1 = "1. Preconstriction & Mobilization";
            String t2 = "2. Constriction Phase";
            String t3 = "3. Operation & Maintenance Phase";
            String pass = "";
            if (indx == 0)
                pass = t1;
            else if (indx == 1)
                pass = t2;
            else if (indx == 2)
                pass = t3;
            //  String yy = comboBoxComponents.SelectedValue.ToString();
            //  string zz = comboBoxComponents.SelectedValuePath.ToString();

            try
            {
                listBoxActivities.Visibility = System.Windows.Visibility.Visible;
                listBoxActivities.ItemsSource = new Func_ProjectActivities().getProjectActivityDetailsByCat(labelProjectReference.Content.ToString(), pass);
            }
            catch (Exception)
            {
                listBoxActivities.Visibility = System.Windows.Visibility.Hidden;
                MessageBox.Show("Select a project to view activities", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void Image_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void comboBoxComponents_MouseEnter(object sender, MouseEventArgs e)
        {
            //if (ProjectAutocompleteTextBox.Text == null)
            //    MessageBox.Show("Select a project");

        }
    }
}
